package com.woniuxy.intellihome.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.intellihome.entity.*;
import com.woniuxy.intellihome.service.ReviewService;
import com.woniuxy.intellihome.service.SkuService;
import com.woniuxy.intellihome.service.UserService;
import com.woniuxy.intellihome.service.VgoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-08-11
 */
@RestController
@RequestMapping("admin/review")
public class ReviewController {


    @Autowired
    private ReviewService reviewService;
    @Autowired
    private UserService userService;
    @Autowired
    private VgoodsService vgoodsService;

    @PostMapping("list")
    public ResponseEntity<List<Review>> list(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, @RequestBody Review review, BindingResult result) {
        ResponseEntity responseEntity = new ResponseEntity();

        if (review == null) {
            review = new Review();
        }
        QueryWrapper<Review> queryWrapper = new QueryWrapper<>();
        if (review.getType() != null && review.getType().equals("1")) {
            queryWrapper.eq("type", "1");
        }
        if (review.getType() != null && review.getType().equals("2")) {
            queryWrapper.eq("type", "2");
        }
        if (review.getType() != null && review.getType().equals("3")) {
            queryWrapper.eq("type", "3");
        }
        if (review.getStartTime() != null && review.getStartTime().trim().length() != 0) {
            queryWrapper.ge("time", review.getStartTime());
        }
        if (review.getEndTime() != null && review.getEndTime().trim().length() != 0) {
            queryWrapper.le("time", review.getEndTime());
        }

        Page<Review> page = new Page<>(current, pageSize);
        //检查是否存在错误
        if (result.hasFieldErrors()) {
            //不存在返回无效提示
            Map<String, String> msgs = new HashMap<>();
            result.getFieldErrors().forEach(e -> {
                msgs.put(e.getField(), e.getDefaultMessage());
            });
            responseEntity.setCode(ResponseState.INVALIDARGS);
            responseEntity.setMsg("无效参数");
            responseEntity.setData(msgs);
        } else {

            //查询数据中所有评论分页信息，并返回JSON语句

            //返回JSON数据
            page = reviewService.page(page, queryWrapper);
            //查询全部评论附带前端需要的信息

            List<Review> reviews1 = page.getRecords();
            //类型转换，防止下面的add方法报错
            List<Review> reviews=new ArrayList<>(reviews1);
            for (Review r : reviews) {
                r.setUser(userService.getById(r.getUserId()));
                r.setUserAccount(userService.getAccountById(r.getUserId()));
                List<VGoods> vGoodses = vgoodsService.getDetailById(r.getSkuId());
                List<String> names = new ArrayList<>();
                for (VGoods vGoods : vGoodses) {
                    names.add(vGoods.getName());
                }
                for (int i = 0; i < names.size(); i++) {
                    for (int j = 0; j < names.size(); j++) {
                        if (i != j && names.get(i).equals(names.get(j))) {
                            names.remove(names.get(j));
                        }
                    }
                }
                String goodsName = names.get(0);
                r.setGoodsName(goodsName);
                r.setSpecValues(vgoodsService.getAllSpecValues(r.getSkuId()));
                r.setVGoodses(vGoodses);
            }

            if (reviews.size() == 0) {
                responseEntity.setMsg("查询成功");
                responseEntity.setCode(ResponseState.FAIL);
            } else {
                responseEntity.setMsg("查询成功");
                responseEntity.setCode(ResponseState.SUCCESS);
            }
            reviews.add(0, review);
            page.setRecords(reviews);
            responseEntity.setData(page);

        }
        return responseEntity;
    }

    @GetMapping("delete/{id}")
    public ResponseEntity delete(@PathVariable int id) {
        reviewService.delete(id);
        return ResponseEntity.SUCCESS;
    }
}

